package magis;

import edu.byu.intex2.data.Controller;
import edu.byu.intex2.data.GUID;
import edu.byu.intex2.data.bo.Sale;
import edu.byu.intex2.data.bo.TradeInLine;
import edu.byu.intex2.data.bo.UsedCar;
import edu.byu.intex2.data.dao.TradeInLineDAO;
import edu.byu.intex2.data.dao.UsedCarDAO;

/**
 * JFrame that handles the creation of a new TradeInLine
 * @author Brady
 */
public class AddTradeInLine extends javax.swing.JFrame {

    private Sale sale;
    private TradeInLineTableDataModel tiltdm;
    private SaleItemsTableDataModel sitdm;
    private UsedCar car;

    /** Creates new form AddTradeInLine */
    public AddTradeInLine(Sale sale, TradeInLineTableDataModel tiltdm, SaleItemsTableDataModel sitdm) {
        initComponents();

        this.sale = sale;
        this.tiltdm = tiltdm;
        this.sitdm = sitdm;

        Controller.getInstance().center(this);
    }

    /** Calculate the UsedCarValue via Web Services */
    private int getUsedCarValue() {
        if (year_txt.getText().equals("") ||
                make_txt.getText().equals("") ||
                make_txt.getText().equals("") ||
                model_txt.getText().equals("") ||
                color_txt.getText().equals("") ||
                mileage_txt.getText().equals("") ||
                vin_txt.getText().equals("")) {
            errorMessage_lbl.setText("All Fields Required");
        }
        try { // Call Web Service Operation
            usedcarpricer.usedcarpricer.UsedCarPricer_Service service = new usedcarpricer.usedcarpricer.UsedCarPricer_Service();
            usedcarpricer.usedcarpricer.UsedCarPricer port = service.getUsedCarPricer();

            // initialize WS operation arguments here
            String make = make_txt.getText();
            String model = model_txt.getText();
            int year = Integer.parseInt(year_txt.getText());
            int condition = Integer.parseInt(condition_cmb.getSelectedItem().toString()); // -1 = bad, 0 = good, 1 = excellent

            // process result here
            int result = port.priceUsedCar(make, model, year, condition);
            return result;
        } catch (Exception ex) {
            // handle custom exceptions here
        }
        return 0; // return nothing (error)
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        saleCarGroup = new javax.swing.JPanel();
        mileage_lbl = new javax.swing.JLabel();
        tradeInValueDisp_lbl = new javax.swing.JLabel();
        model_lbl = new javax.swing.JLabel();
        make_lbl = new javax.swing.JLabel();
        vin_txt = new javax.swing.JTextField();
        color_lbl = new javax.swing.JLabel();
        year_lbl = new javax.swing.JLabel();
        listPrice_lbl = new javax.swing.JLabel();
        addCar_btn3 = new javax.swing.JButton();
        year_txt = new javax.swing.JTextField();
        make_txt = new javax.swing.JTextField();
        model_txt = new javax.swing.JTextField();
        color_txt = new javax.swing.JTextField();
        mileage_lbl1 = new javax.swing.JLabel();
        mileage_txt = new javax.swing.JTextField();
        condition_cmb = new javax.swing.JComboBox();
        year_lbl1 = new javax.swing.JLabel();
        errorMessage_lbl = new javax.swing.JLabel();
        bottomMenu_pnl = new javax.swing.JPanel();
        cancel_btn = new javax.swing.JButton();
        saveUsedCar_btn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        saleCarGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Add Used Car Trade In"));
        saleCarGroup.setPreferredSize(new java.awt.Dimension(450, 107));
        saleCarGroup.setLayout(new java.awt.GridBagLayout());

        mileage_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        mileage_lbl.setText("Mileage:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(mileage_lbl, gridBagConstraints);

        tradeInValueDisp_lbl.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        tradeInValueDisp_lbl.setText("Calc Value");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(tradeInValueDisp_lbl, gridBagConstraints);

        model_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        model_lbl.setText("Model:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(model_lbl, gridBagConstraints);

        make_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        make_lbl.setText("Make:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(make_lbl, gridBagConstraints);

        vin_txt.setFont(vin_txt.getFont().deriveFont(vin_txt.getFont().getSize()-1f));
        vin_txt.setText("12345ANF9012345LL");
        vin_txt.setPreferredSize(new java.awt.Dimension(120, 19));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(vin_txt, gridBagConstraints);

        color_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        color_lbl.setText("Color:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(color_lbl, gridBagConstraints);

        year_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        year_lbl.setText("Year:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(year_lbl, gridBagConstraints);

        listPrice_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        listPrice_lbl.setText("Value:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(listPrice_lbl, gridBagConstraints);

        addCar_btn3.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        addCar_btn3.setText("Get Used Car Value");
        addCar_btn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCar_btn3ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(addCar_btn3, gridBagConstraints);

        year_txt.setFont(year_txt.getFont().deriveFont(year_txt.getFont().getSize()-2f));
        year_txt.setText("2006");
        year_txt.setPreferredSize(new java.awt.Dimension(45, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(year_txt, gridBagConstraints);

        make_txt.setFont(make_txt.getFont().deriveFont(make_txt.getFont().getSize()-2f));
        make_txt.setText("Ford");
        make_txt.setPreferredSize(new java.awt.Dimension(45, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(make_txt, gridBagConstraints);

        model_txt.setFont(model_txt.getFont().deriveFont(model_txt.getFont().getSize()-2f));
        model_txt.setText("Ranger");
        model_txt.setPreferredSize(new java.awt.Dimension(45, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(model_txt, gridBagConstraints);

        color_txt.setFont(color_txt.getFont().deriveFont(color_txt.getFont().getSize()-2f));
        color_txt.setText("White");
        color_txt.setPreferredSize(new java.awt.Dimension(45, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(color_txt, gridBagConstraints);

        mileage_lbl1.setFont(new java.awt.Font("Tahoma", 0, 9));
        mileage_lbl1.setLabelFor(condition_cmb);
        mileage_lbl1.setText("Condition:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(mileage_lbl1, gridBagConstraints);

        mileage_txt.setFont(mileage_txt.getFont().deriveFont(mileage_txt.getFont().getSize()-2f));
        mileage_txt.setText("46000");
        mileage_txt.setPreferredSize(new java.awt.Dimension(45, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(mileage_txt, gridBagConstraints);

        condition_cmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-1", "-0", "1" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(condition_cmb, gridBagConstraints);

        year_lbl1.setFont(new java.awt.Font("Tahoma", 0, 9));
        year_lbl1.setText("VIN:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 3, 4);
        saleCarGroup.add(year_lbl1, gridBagConstraints);

        errorMessage_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        errorMessage_lbl.setForeground(new java.awt.Color(153, 0, 0));
        errorMessage_lbl.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 0);
        saleCarGroup.add(errorMessage_lbl, gridBagConstraints);

        getContentPane().add(saleCarGroup, java.awt.BorderLayout.CENTER);

        cancel_btn.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        cancel_btn.setText("Cancel");
        cancel_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_btnActionPerformed(evt);
            }
        });
        bottomMenu_pnl.add(cancel_btn);

        saveUsedCar_btn.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        saveUsedCar_btn.setText("Save Used Car");
        saveUsedCar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveUsedCar_btnActionPerformed(evt);
            }
        });
        bottomMenu_pnl.add(saveUsedCar_btn);

        getContentPane().add(bottomMenu_pnl, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addCar_btn3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCar_btn3ActionPerformed
        // Get the TradeInValue and display it
        tradeInValueDisp_lbl.setText(String.valueOf(getUsedCarValue()));
    }//GEN-LAST:event_addCar_btn3ActionPerformed

    private void cancel_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_btnActionPerformed
        this.dispose();
}//GEN-LAST:event_cancel_btnActionPerformed

    private void saveUsedCar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveUsedCar_btnActionPerformed
        // Validate TradeInLine
        if (year_txt.getText().equals("") ||
                make_txt.getText().equals("") ||
                make_txt.getText().equals("") ||
                model_txt.getText().equals("") ||
                color_txt.getText().equals("") ||
                mileage_txt.getText().equals("") ||
                tradeInValueDisp_lbl.getText().equals("Calc Value") ||
                vin_txt.getText().equals("")) {
            errorMessage_lbl.setText("All Fields Required");
        }

        // Create the UsedCar
        try {
            car = UsedCarDAO.getInstance().create(GUID.generate());
            
            // Set Used Car Attributes
//            car.setMake(make_txt.getText());
//            car.setModel(model_txt.getText());
//            car.setYear(Integer.parseInt(year_txt.getText()));
            car.setCarCondition(condition_cmb.getSelectedItem().toString()); // -1 = bad, 0 = good, 1 = excellent
            car.setMileage(Integer.parseInt(mileage_txt.getText()));
            car.setVin(vin_txt.getText());

            // Save our used car to the DB. -- TODO: this will cause rollbacks not to work.
            car.save();

        } catch (Exception e) {
            e.printStackTrace();
        }

        // Add the New Trade In Line
        try {
            TradeInLine til = TradeInLineDAO.getInstance().create(sale.getId(), car.getVin());

            // Set TradeInLine attributes
            til.setUsedCar(car);
            til.setTradeInValue(Double.valueOf(tradeInValueDisp_lbl.getText()));
            til.setMileage(car.getMileage());

            // Add Trade In Line to Sale
            sale.addTradeInLine(til);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Tell jtableModel we have changed the table data
        tiltdm.fireTableDataChanged();
        sitdm.fireTableDataChanged();
        this.dispose(); // Close Window
    }//GEN-LAST:event_saveUsedCar_btnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
//                new AddTradeInLine().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCar_btn3;
    private javax.swing.JPanel bottomMenu_pnl;
    private javax.swing.JButton cancel_btn;
    private javax.swing.JLabel color_lbl;
    private javax.swing.JTextField color_txt;
    private javax.swing.JComboBox condition_cmb;
    private javax.swing.JLabel errorMessage_lbl;
    private javax.swing.JLabel listPrice_lbl;
    private javax.swing.JLabel make_lbl;
    private javax.swing.JTextField make_txt;
    private javax.swing.JLabel mileage_lbl;
    private javax.swing.JLabel mileage_lbl1;
    private javax.swing.JTextField mileage_txt;
    private javax.swing.JLabel model_lbl;
    private javax.swing.JTextField model_txt;
    private javax.swing.JPanel saleCarGroup;
    private javax.swing.JButton saveUsedCar_btn;
    private javax.swing.JLabel tradeInValueDisp_lbl;
    private javax.swing.JTextField vin_txt;
    private javax.swing.JLabel year_lbl;
    private javax.swing.JLabel year_lbl1;
    private javax.swing.JTextField year_txt;
    // End of variables declaration//GEN-END:variables
}
